package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */
class ShoppingCartManagement {
    
    def listShoppingCarts() {
        def carts = ShoppingCart.list()
        JSONArray arr = new JSONArray()
        carts.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("creationDate",it.creationDate)
            obj.put("modifiedDate",it.modifiedDate)
            obj.put("customerId",it.customer.id)
            obj.put("uniqueKey",it.uniqueKey)
            obj.put("cartItems",it.cartItems)
            
            arr.add(obj)
        }
        
        return arr
    }
    
    def getShoppingCartByID(id) {
        def sc = ShoppingCart.findById(id)
        JSONObject obj = new JSONObject()
        obj.put("id",sc.id)
        obj.put("creationDate",sc.creationDate)
        obj.put("modifiedDate",sc.modifiedDate)
        obj.put("customerId",sc.customer.id)
        obj.put("cartItems",sc.cartItems)
        obj.put("uniqueKey",sc.uniqueKey)
        return obj
    }
    
    def getShoppingCartByCustomerID(customerId) {
        def sc = ShoppingCart.findAll()
        if (sc.size () > 0) {
            sc.each() {
                if (it.customer.id.toInteger() == customerId.toInteger()) {
                    JSONObject obj = new JSONObject()
                    obj.put("id",it.id)
                    obj.put("creationDate",it.creationDate)
                    obj.put("modifiedDate",it.modifiedDate)
                    obj.put("customerId",it.customer.id)
//            //        obj.put("cartItems",sc.cartItems)
                    obj.put("uniqueKey",it.uniqueKey)
                    return obj
                }
            }
        }
        else {
            return null
        }
        
    }
    
    def getShoppingCartForDbByCartID(id) {
        println 'getShoppingCartForDbByCartID'
        def sc = ShoppingCart.findById(id)
        println sc
        return sc
    }
    
    def addShoppingCart(customer, key) {
        def status
        
        ShoppingCart sc = new ShoppingCart()
        Date cd = new Date()
        sc.creationDate = cd
        Date md = new Date()
        sc.modifiedDate = md
        sc.customer = customer
        sc.uniqueKey = key
        
        if (sc.save()) {
            status = true
            return sc
        }
        else {
            status = false
        }        
        println 'Management >> Add ShoppingCart >> ' + status
        
        return status
    }
    
    def updateShoppingCart(cart) {
        def status
        
        ShoppingCart sc = ShoppingCart.findById(cart.id)
        if (cart.has("cartItems")) { 
            /// TODO: make list then add
            sc.cartItems = cart.cartItems 
        }

        if (sc.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Update ShoppingCart >> ' + status
        
        return status
    }
    
    def deleteShoppingCart(id) {
        def status
        
        ShoppingCart sc = ShoppingCart.findById(id)
        if (sc.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Delete ShoppingCart >> ' + status
        
        return status
    }
}

